<?php

/**
 * product actions.
 *
 * @package    auction
 * @subpackage product
 * @author     Do Anh Tu
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class productActions extends sfActions
{
  public function executeHistory(sfWebRequest $request)
  {
    $this->pid = $request->getParameter('pid');
    $this->product = Doctrine_Core::getTable('Product')->find($this->pid);
    if (!$this->product) {
      $this->forward404();
    }
    $this->bids = $this->product->Bids;
  }

  public function executeBid(sfWebRequest $request)
  {
    if ($this->getUser()->isAuthenticated()) {
      $user = $this->getUser()->getGuardUser();
      $bids_left = $user->getProfile()->getBidsLeft();
      if ($bids_left > 0) {
        $this->pid = $request->getParameter('pid');
        $this->product = Doctrine_Core::getTable('Product')->find($this->pid);
        if (!$this->product) {
          $this->forward404();
        }
        $user->getProfile()->makeBid()->save();
        $this->product->makeBid()->save();
        $bid = new Bid(null, true);
        $bid->setProductId($this->pid);
        $bid->setPrice($this->product->getCurrentPrice());
        $bid->setCreatedBy($user->getId());
        $bid->save();
        $this->success = true;
      } else {
        $this->success = false;
      }
    }
  }

  public function executeIndex(sfWebRequest $request)
  {
    $this->products = Doctrine_Core::getTable('Product')
      ->createQuery('a')
      ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->product = Doctrine_Core::getTable('Product')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->product);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new ProductForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new ProductForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($product = Doctrine_Core::getTable('Product')->find(array($request->getParameter('id'))), sprintf('Object product does not exist (%s).', $request->getParameter('id')));
    $this->form = new ProductForm($product);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($product = Doctrine_Core::getTable('Product')->find(array($request->getParameter('id'))), sprintf('Object product does not exist (%s).', $request->getParameter('id')));
    $this->form = new ProductForm($product);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($product = Doctrine_Core::getTable('Product')->find(array($request->getParameter('id'))), sprintf('Object product does not exist (%s).', $request->getParameter('id')));
    $product->delete();

    $this->redirect('product/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $product = $form->save();

      $this->redirect('product/edit?id='.$product->getId());
    }
  }
}
